const path = require('path')
const webpack = require('webpack');
const {GitRevisionPlugin} = require("git-revision-webpack-plugin");
const GitRevision = new GitRevisionPlugin();
const buildDate = JSON.stringify(new Date().toLocaleString());

const resolve = (dir) => {
  return path.join(__dirname, dir)
}
const name = 'vue Admin Template' // page title
const argv = require('yargs').argv
let publicPath = process.env.NODE_ENV === 'development' ? '/' : '/paservice/'
if (argv.virtualDir) {
  publicPath = '/' + argv.virtualDir + publicPath;
}
// 打包时终端验证下publicPath
console.log('public path is', publicPath);

// module.exports = {
 
// }

module.exports = {

  publicPath: publicPath,
  outputDir: 'paservice',
  assetsDir: 'assets',
  indexPath: 'index.html',
  filenameHashing: true,
  pages: {
    index: {
      entry: 'src/main.js',
      template: 'public/index.html',
      filename: 'index.html'
      // title: 'Index Page',
      // chunks: ['chunk-vendors', 'chunk-common', 'index']
    }
  },
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false, // 除去.map文件
  devServer: {
    host: 'localhost',
    port: '8080',
    hot: false,
    open: true,
    host: '0.0.0.0',
    // overlay: {
    //   warning: false,
    //   error: true
    // }
    proxy: process.env.NODE_ENV === 'development' ? {
      '/api/*': {
        target: 'http://api.eworlds.com.cn',
        changeOrigin: true,
        pathRewrite: { '^/api/': '' }
      }
    } : {},
    // proxy: process.env.NODE_ENV === 'development' ? {
    //   '/api/*': {
    //     target: 'http://api-dev.mtywcloud.com',
    //     changeOrigin: true,
    //     pathRewrite: { '^/api/': '' }
    //   }
    // } : {},
    historyApiFallback: {
      rewrites: [{
        from: /.*/g,
        to: '/index.html' //与output的publicPath有关(HTMLplugin生成的html默认为index.html)
      }]
    },
  },
  configureWebpack: config=>{
    if(process.env.NODE_ENV === 'production'){

      return {
        plugins:[
          new webpack.DefinePlugin({
            APP_VERSION: `"${require("./package.json").version}"`,
            BUILD_DATE: buildDate,
            BRANCH: JSON.stringify(GitRevision.branch()),
            COMMITHASH: JSON.stringify(GitRevision.commithash()),
            VERSION: JSON.stringify(GitRevision.version())
          })
        ],
        name: name,
        resolve: {
          alias: {
            '@': resolve('src')
          }
        }
      }
    }else{
      return {
        name: name,
        resolve: {
          alias: {
            '@': resolve('src')
          }
        }
      }
    }
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
   
  },
  chainWebpack: config => {
    config.plugin("copy").tap(([options]) => {
      options[0].ignore.push("oidc/**");
      options[0].ignore.push("config/**");
      return [options];
    });
    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map'))
  }
}
